<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="状态">
                <a-select v-model="queryParam.status" placeholder="请选择状态">
                  <a-select-option v-for="it in statusList" :key="it.id" :value="it.id">{{ it.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            
            <a-col :md="8" :sm="24">
              <a-form-item label="类型">
                <a-select v-model="queryParam.type" placeholder="请选择类型">
                  <a-select-option v-for="it in templateTypeList" :key="it.id" :value="it.id">{{ it.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="平台">
                  <!-- platformId -->
                  <AWM-select
                    v-model="queryParam.platformId"
                    placeholder="请选择平台"
                    funStr="platformByNoAuth"
                  ></AWM-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="产品">
                  <!-- platformId -->
                  <AWM-select
                    v-model="queryParam.productId"
                    placeholder="请选择产品"
                    funStr="productByNoAuth"
                  ></AWM-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="用户">
                  <!-- platformId -->
                  <AWM-select
                    v-model="queryParam.userId"
                    placeholder="请选择用户"
                    funStr="userListByNoAuth"
                  ></AWM-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col  :md="(!advanced && 8) || 24" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => ((this.queryParam = {}), (this.need = {}))"
                  >重置</a-button
                >
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button>
      </div>
      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
        :scroll="{ x: 'auto' }"
      > 
      
        <span slot="imageUrl" slot-scope="text, record">
          <div class="img-wrap">
            <div class="img-wrap-item" v-if="record.licenseFile">
              
              <img :src="`${baseUrl}/${record.licenseFile}`"  alt="">
              <div>{{record.signType == 3?'个人身份证正面':'营业执照'}}</div>
            </div>
            <div class="img-wrap-item" v-if="record.mandateFile">
              
              <img :src="`${baseUrl}/${record.mandateFile}`" alt="">
              <div>授权文件</div>
            </div>
            <div class="img-wrap-item" v-if="record.mandateLicenseFile">
              
              <img :src="`${baseUrl}/${record.mandateLicenseFile}`" alt="">
              <div>{{record.signType == 3?'个人身份证反面':'授权单位营业执照'}}</div>
            </div>
            
          </div>
          
          
          
        </span>
        <span slot="status" slot-scope="text">
          <a-tag :color="text | statusFilter(statusList, 'status')">{{
            text | statusFilter(statusList, 'name')
          }}</a-tag>
        </span>
        <span slot="signType" slot-scope="text">
          <a-badge style="font-size:10px;"  :color="text | statusFilter(signTypeList, 'status')" :text="text | statusFilter(signTypeList, 'sort')" />
        </span>
        <span slot="amount" slot-scope="text">￥{{ text | NumberFormat }}</span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a
              @click="handleEdit(record)"
              >编辑</a
            >
            <a-divider type="vertical" />
            <a @click="handleDel(record)">删除</a>
          </template>
        </span>
        <span slot="remark" slot-scope="text">
          <template>
            <div class="remark-wrap">{{ text }}</div>
          </template>
        </span>
      </s-table>
      <signature-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import AWMSelect from '@/components/AWMSelect'
import { signatureAdd,
signatureList,
signatureUpdate } from '@/api/sms'
import { msgStatusList, signatureStatusList, signTypeList, templateTypeList } from '@/utils/other'
// import StepByStepModal from './modules/StepByStepModal'
import { mapGetters } from 'vuex'
import SignatureForm from './modules/SignatureForm.vue'

// id，发起用户，手机号码，标题，内容，图片附件，请求id，添加时间，定时发送，状态
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    key: 'id',
  },
  {
    title: '签名',
    dataIndex: 'name',
    key: 'name',
    width: '130px',
    customRender: (text) => text || '--',
  },
   {
    title: '用户',
    dataIndex: 'userName',
    key: 'userName',
    width: '180px',
    customRender: (text, record) => `${text}(${record.userId})` || '--',
  },
  {
    title: '产品',
    dataIndex: 'productName',
    width: 180,
  },
  {
    title: '平台',
    width: '150px',
    dataIndex: 'platformName',
  },
  {
    title: '签名用途',
    width: '150px',
    dataIndex: 'signType',
    scopedSlots: { customRender: 'signType' },
  },
  {
    title: '图片附件',
    dataIndex: 'imageUrl',
    width: '200px',
    scopedSlots: { customRender: 'imageUrl' },
  },
  {
    title: '添加时间',
    dataIndex: 'addTime',
    width: '180px',
  },
  // {
  //   title: '定时发送',
  //   dataIndex: 'timing',
  //   width: '200px',
  //   customRender: (text) => text || '--',
  // },
  {
    title: '状态',
    dataIndex: 'status',
    width: '90px',
    scopedSlots: { customRender: 'status' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' },
  },
]

export default {
  name: 'log-list',
  components: {
    STable,
    SignatureForm,
    AWMSelect
  },
  data() {
    this.columns = columns
    return {
      open: false,
      need: {},
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 查询参数
      parameter: {
        pageNum: 1,
        pageSize: 10,
      },
      advanced:false,
      queryParam: {
      },
      data: [],
      // 状态
      statusList: signatureStatusList,
      signTypeList,
      templateTypeList,
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam )
        // requestParameters.addUser = this.$store.getters.userInfo.id || 0
        return signatureList(requestParameters).then((res) => {
          let result = {
            data: res.data.data,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalCount: res.data.total,
            totalPage: res.data.pages,
          }
          return result
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
    }
  },
  created() {},
  computed: {
    ...mapGetters({ baseUrl: 'baseUrl', userInfo: 'userInfo' }),
    isTiming: function () {
      console.log(this.$route.path.indexOf('/timing'))
      return this.$route.path.indexOf('/timing') != -1
    },
    // isInternational: function () {
    //   return this.$route.path.indexOf('/international/') != -1 ? 2 : 1
    // },
    // computedQueryParam: function () {
    //   return {
    //     isInland: this.isInternational,
    //   }
    // },
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleCheckList(record) {
      this.check.mdl = { ...record }
      this.check.visible = true
    },
    handleAdd() {
      this.mdl = null
      this.visible = true
    },

    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
    },
    saveCB() {
      const form = this.$refs.createModal.form
      this.visible = false
      this.confirmLoading = false
      // 重置表单数据
      form.resetFields()
      // 刷新表格
      this.$refs.table.refresh()
    },
    handleOk() {
      let self = this;
      const modal = this.$refs.createModal
      const form = modal.form
      this.confirmLoading = true
      console.log(111);
      form.validateFields((errors, values) => {
        console.log(errors, values);
        if (!errors) {
          values.licenseFile = modal.licenseFile.file
            values.mandateLicenseFile = modal.mandateLicenseFile.file
            values.mandateFile = modal.mandateFile.file
          if (values.id > 0) {
            
            signatureUpdate(values)
              .then((res) => {
                this.saveCB()
                this.$message.success('修改成功!')
              })
              .catch((err) => {
                this.saveCB()
              })
          } else {
            

            console.log(222);
            delete values.id
            signatureAdd(values)
              .then((res) => {
                this.saveCB()
                this.$message.success('新增成功!')
              })
              .catch((err) => {
                this.saveCB()
              })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleDel(a_record) {
      let self = this
      const modal = self.$confirm({
        title: '用户删除',
        content: `是否删除用户:${a_record.name}`,
        okText: '确认',
        cancelText: '取消',
        onOk: () => self.handleDelete(a_record.id, modal, -1),
        onCancel: () => {},
      })
    },
    handleDelete(a_id, a_modal, a_type) {
      return new Promise((resolve, reject) => {
        let data = { id: a_id, status: a_type }
        signatureUpdate(data)
          .then((res) => {
            this.$refs.table.refresh()
            this.$message.success('删除成功!')
            resolve()
          })
          .catch((err) => {
            a_modal.destroy()
            this.$message.error('操作失败!')
          })
      })
    },
  },
}
</script>
<style scoped lang="less">
.param-wrap {
  min-width: 200px;
  max-width: 300px;
}

.a-tag-wrap {
  font-size: 12px;
  /deep/ .ant-tag {
    line-height: 18px;
    padding: 0 4px;
  }
  div {
    margin-bottom: 2px;
    &:last-child {
      margin-bottom: 0;
    }
  }
}
.img-wrap{
  display: flex;
  .img-wrap-item{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 60px;
    padding: 5px;
    margin-right:5px;
    // background-color: #ccc;
    img{
      max-width: 50px;
      max-height: 50px;
    }
    div{
      font-size:10px;
      text-align: center;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}
</style>

 